<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="线路类型名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="线路类型名称"></el-input>
    </el-form-item>
    <el-form-item label="线路类型编号" prop="typeNumber">
      <el-input v-model="dataForm.typeNumber" placeholder="线路类型编号"></el-input>
    </el-form-item>
    <el-form-item label="起始地机构类型" prop="startAgencyType">
      <el-input v-model="dataForm.startAgencyType" placeholder="起始地机构类型"></el-input>
    </el-form-item>
    <el-form-item label="目的地机构类型" prop="endAgencyType">
      <el-input v-model="dataForm.endAgencyType" placeholder="目的地机构类型"></el-input>
    </el-form-item>
    <el-form-item label="最后更新时间" prop="lastUpdateTime">
      <el-input v-model="dataForm.lastUpdateTime" placeholder="最后更新时间"></el-input>
    </el-form-item>
    <el-form-item label="最后更新人" prop="updater">
      <el-input v-model="dataForm.updater" placeholder="最后更新人"></el-input>
    </el-form-item>
    <el-form-item label="状态  0：禁用   1：正常" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态  0：禁用   1：正常"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          typeNumber: '',
          startAgencyType: '',
          endAgencyType: '',
          lastUpdateTime: '',
          updater: '',
          status: ''
        },
        dataRule: {
          name: [
            { required: true, message: '线路类型名称不能为空', trigger: 'blur' }
          ],
          typeNumber: [
            { required: true, message: '线路类型编号不能为空', trigger: 'blur' }
          ],
          startAgencyType: [
            { required: true, message: '起始地机构类型不能为空', trigger: 'blur' }
          ],
          endAgencyType: [
            { required: true, message: '目的地机构类型不能为空', trigger: 'blur' }
          ],
          lastUpdateTime: [
            { required: true, message: '最后更新时间不能为空', trigger: 'blur' }
          ],
          updater: [
            { required: true, message: '最后更新人不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态  0：禁用   1：正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/base/transportlinetype/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.transportLineType.name
                this.dataForm.typeNumber = data.transportLineType.typeNumber
                this.dataForm.startAgencyType = data.transportLineType.startAgencyType
                this.dataForm.endAgencyType = data.transportLineType.endAgencyType
                this.dataForm.lastUpdateTime = data.transportLineType.lastUpdateTime
                this.dataForm.updater = data.transportLineType.updater
                this.dataForm.status = data.transportLineType.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/base/transportlinetype/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'typeNumber': this.dataForm.typeNumber,
                'startAgencyType': this.dataForm.startAgencyType,
                'endAgencyType': this.dataForm.endAgencyType,
                'lastUpdateTime': this.dataForm.lastUpdateTime,
                'updater': this.dataForm.updater,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
